package com.txzt.gaea.provide.utils;

import com.alibaba.druid.util.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.*;

public class HttpClientUtils {

    private static PoolingHttpClientConnectionManager connectionManager = null;
    private static HttpClientBuilder httpBuilder = null;

    private static RequestConfig requestConfig = null;

    private static final int MAXCONNECTION = 10;
    private static final int DEFAULTMAXCONNECTION = 5;
    private static final String IP = "192.168.1.80";
    private static final int PORT = 8989;

    private static final String POST = "post";
    private static final String GET = "get";

    static {

        requestConfig = RequestConfig.custom()
                .setSocketTimeout(5000)
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .build();

        HttpHost targert = new HttpHost(IP, PORT);

        connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(MAXCONNECTION);
        connectionManager.setDefaultMaxPerRoute(DEFAULTMAXCONNECTION);

        connectionManager.setMaxPerRoute(new HttpRoute(targert), 20);
        httpBuilder = HttpClients.custom();
        httpBuilder.setConnectionManager(connectionManager);



    }


    public static CloseableHttpClient getConnection(){
        CloseableHttpClient httpClient = httpBuilder.build();
        return httpClient;
    }



    public static HttpUriRequest getRequestMethod(Map<String, String> map, String url, String method){
        List<NameValuePair> paramsN = convertParameters(map);
        HttpUriRequest requestMethod = null;
        if(StringUtils.equals(POST, method)){
            requestMethod = RequestBuilder.post().setUri(url)
                    .addParameters(paramsN.toArray(new BasicNameValuePair[paramsN.size()]))
                    .addHeader("toolCode", "smart_work")
                    .addHeader("authCode", "819f1029984a4eaaad8c67f3e602c852")
                    .setConfig(requestConfig).build();
        } else if(StringUtils.equals(GET, method)){
            requestMethod = RequestBuilder.get().setUri(url)
                    .addHeader("toolCode", "smart_work")
                    .addHeader("authCode", "819f1029984a4eaaad8c67f3e602c852")
                    .addParameters(paramsN.toArray(new BasicNameValuePair[paramsN.size()]))
                    .setConfig(requestConfig).build();
        }
        return requestMethod;

    }

    public static List<NameValuePair> convertParameters(Map<String, String> map){
        List<NameValuePair> params = new ArrayList<>();
        Set<Map.Entry<String, String>> entrySet = map.entrySet();
        for(Map.Entry<String, String> e: entrySet){
            String name = e.getKey();
            String value = e.getValue();
            NameValuePair pair = new BasicNameValuePair(name, value);
            params.add(pair);
        }
        return params;
    }






    public static void main(String[] args) throws Exception{

//        Map<String, String> map = new HashMap<>();
//        map.put("fileId", "66a877f04faa4c49a99b5015892fa223");
//        HttpClient client = getConnection();
//        HttpUriRequest post = getRequestMethod(map, "http://192.168.1.80:8989/oss/queryUrlByFileId", "post");
//        HttpResponse response = client.execute(post);
//
//        if(response.getStatusLine().getStatusCode() == 200){
//            HttpEntity entity = response.getEntity();
//            String message = EntityUtils.toString(entity, "utf-8");
//
//            JSONObject jsonObject = JSONObject.parseObject(message);
//
//            JSONObject data = jsonObject.getJSONObject("data");
//
//            String accessUrl = data.getString("accessUrl");
//
//
//            System.out.println(message);
//        }else{
//            System.out.println("请求失败");
//        }

        Map<String, String> map = new HashMap<>();
        map.put("fileId", "66a877f04faa4c49a99b5015892fa223");
        HttpClient client = getConnection();
        HttpUriRequest post = getRequestMethod(map, "http://192.168.1.80:8989/oss/download", "post");
        HttpResponse response = client.execute(post);

        if(response.getStatusLine().getStatusCode() == 200){
            HttpEntity entity = response.getEntity();
            String message = EntityUtils.toString(entity, "utf-8");

            System.out.println(message);
        }else{
            System.out.println("请求失败");
        }





    }



}
